package com.fr.lintcode;

import java.util.Stack;

import org.junit.Test;

/**
*作者：furong
*日期：2017年2月8日
*时间：上午10:36:54
*/
public class Q12 {
    public class MinStack {
        Stack<Integer> s;
        Stack<Integer> min;

        public MinStack() {
            s = new Stack<>();
            min = new Stack<>();
        }

        public void push(int number) {
            s.push(number);
            if (min.empty()) {
                min.push(number);
            } else if (min.peek() > number) {
                min.push(number);
            } else {
                min.push(min.peek());
            }
        }

        public int pop() {
            if (s.empty()) {
                return 0;
            } else {
                min.pop();
                return s.pop();
            }
        }

        public int min() {
            if (min.empty()) {
                return 0;
            }
            return min.peek();
        }
    }

    @Test
    public void testA() {
        MinStack ms = new MinStack();
        ms.push(1);
        System.out.println(ms.pop());
        ms.push(2);
        ms.push(3);
        System.out.println(ms.min());
        ms.push(1);
        System.out.println(ms.min());
    }
}
